package mediator;

import java.util.HashMap;
import java.util.Map;

public class User {
    String name;
    Map<String, ChatRoom> map;

    public User(String name) {
        this.name = name;
        this.map = new HashMap<>();
    }

    public void addToChatRoom(ChatRoom chatRoom) {
        map.put(chatRoom.getName(), chatRoom);
        chatRoom.addUser(this);
    }

    public void quitChatRoom(String chatRoomName) {
        ChatRoom chatRoom = map.get(chatRoomName);
        map.remove(chatRoomName);
        if (chatRoom != null) {
            chatRoom.deleteUser(this);
        }
    }


    public void sendMessage(String message, String chatRoomName) {
        ChatRoom chatRoom = map.get(chatRoomName);
        if (chatRoom != null) {
            chatRoom.sendMessage(message);
        }
    }

    public void receiveMessage(String message) {
        System.out.println(this.name + " received " + message);
    }

}
